package com.ruoyi.place.controller;

import java.util.List;

import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.house.domain.XlkjHouse;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.utils.DictStrUtils;
import com.ruoyi.utils.UrlUtils;
import org.apache.tomcat.util.buf.UriUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.place.domain.XlkjPlace;
import com.ruoyi.place.service.IXlkjPlaceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 行业场所Controller
 *
 * @author wang
 * @date 2022-01-12
 */
@RestController
@RequestMapping("/place/place")
public class XlkjPlaceController extends BaseController
{
    @Autowired
    private IXlkjPlaceService xlkjPlaceService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 查询行业场所列表
     */
    @PreAuthorize("@ss.hasPermi('place:place:list')")
    @GetMapping("/list")
    public TableDataInfo list(XlkjPlace xlkjPlace)
    {
        //筛选已通过的
        xlkjPlace.setStatus("2");
        startPage();
        if(xlkjPlace.getDepartmentId() != null && xlkjPlace.getDepartmentId() != ""){
            List<String> list = sysDeptMapper.selectIdByParentId(Long.valueOf(xlkjPlace.getDepartmentId()));
            String str = "(";
            for(int i =0;i<list.size();i++){
                str += list.get(i);
                if(i != list.size()-1){
                    str += ",";
                }
            }
            str += ")";
            xlkjPlace.setDepartmentList(str);
        }
        List<XlkjPlace> list = xlkjPlaceService.selectXlkjPlaceList(xlkjPlace);
        for(XlkjPlace place: list){
            //图片路径拼接
            if(place.getImg() != null){
                place.setImg(UrlUtils.getUrl() + place.getImg());
            }
            //部门值翻译
            String deptName = sysDeptService.selectDeptById(Long.valueOf(place.getDepartmentId())).getDeptName();
            place.setDepartmentId(deptName);
        }
        return getDataTable(list);
    }

    /**
     * 导出行业场所列表
     */
    @PreAuthorize("@ss.hasPermi('place:place:export')")
    @Log(title = "行业场所", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(XlkjPlace xlkjPlace)
    {
        List<XlkjPlace> list = xlkjPlaceService.selectXlkjPlaceList(xlkjPlace);
        for(XlkjPlace place:list){
            //图片路径拼接
            place.setImg(UrlUtils.getUrl() + place.getImg());
            //字典类型翻译
            place.setCertificatesType(DictUtils.getDictLabel(DictStrUtils.CERTIFICATES_TYPE,place.getCertificatesType()));
            place.setSiteType(DictUtils.getDictLabel(DictStrUtils.SITE_TYPE,place.getSiteType()));
            place.setDangerStatus(DictUtils.getDictLabel(DictStrUtils.SYS_YES_NO,place.getDangerStatus()));
            place.setControlType(DictUtils.getDictLabel(DictStrUtils.CONTROL_TYPE,place.getControlType()));
            place.setRiskType(DictUtils.getDictLabel(DictStrUtils.RISK_TYPE,place.getRiskType()));
            place.setCollectType(DictUtils.getDictLabel(DictStrUtils.COLLECT_TYPE,place.getCollectType()));
        }
        ExcelUtil<XlkjPlace> util = new ExcelUtil<XlkjPlace>(XlkjPlace.class);
        return util.exportExcel(list, "行业场所数据");
    }

    /**
     * 行业场所导入
     */
    @Log(title = "行业场所", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<XlkjPlace> util = new ExcelUtil<XlkjPlace>(XlkjPlace.class);
        List<XlkjPlace> userList = util.importExcel(file.getInputStream());
        for(XlkjPlace place:userList){
            //字典类型翻译
            place.setCertificatesType(DictUtils.getDictValue(DictStrUtils.CERTIFICATES_TYPE,place.getCertificatesType()));
            place.setSiteType(DictUtils.getDictValue(DictStrUtils.SITE_TYPE,place.getSiteType()));
            place.setDangerStatus(DictUtils.getDictValue(DictStrUtils.SYS_YES_NO,place.getDangerStatus()));
            place.setControlType(DictUtils.getDictValue(DictStrUtils.CONTROL_TYPE,place.getControlType()));
            place.setRiskType(DictUtils.getDictValue(DictStrUtils.RISK_TYPE,place.getRiskType()));
            place.setCollectType(DictUtils.getDictValue(DictStrUtils.COLLECT_TYPE,place.getCollectType()));
        }
        String message = xlkjPlaceService.importDevice(userList);
        return AjaxResult.success(message);
    }

    /**
     * 下载行业场所导入模板
     */
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<XlkjPlace> util = new ExcelUtil<XlkjPlace>(XlkjPlace.class);
        return util.importTemplateExcel("行业场所数据");
    }

    /**
     * 获取行业场所详细信息
     */
    @PreAuthorize("@ss.hasPermi('place:place:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(xlkjPlaceService.selectXlkjPlaceById(id));
    }

    /**
     * 新增行业场所
     */
    @PreAuthorize("@ss.hasPermi('place:place:add')")
    @Log(title = "行业场所", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody XlkjPlace xlkjPlace)
    {
        return toAjax(xlkjPlaceService.insertXlkjPlace(xlkjPlace));
    }

    /**
     * 修改行业场所
     */
    @PreAuthorize("@ss.hasPermi('place:place:edit')")
    @Log(title = "行业场所", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody XlkjPlace xlkjPlace)
    {
        return toAjax(xlkjPlaceService.updateXlkjPlace(xlkjPlace));
    }

    /**
     * 删除行业场所
     */
    @PreAuthorize("@ss.hasPermi('place:place:remove')")
    @Log(title = "行业场所", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(xlkjPlaceService.deleteXlkjPlaceByIds(ids));
    }
}
